package com.dartsstats.datasource;

import java.util.ArrayList;
import java.util.List;

import com.dartsstats.Player;
import com.dartsstats.DM.PlayerDM;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PlayerDataSource {
	
	private SQLiteDatabase database;
	private PlayerDM playerDM;
	private String[] allColumns = { PlayerDM.COLUMN_ID, PlayerDM.COLUMN_FIRST_NAME, PlayerDM.COLUMN_LAST_NAME, PlayerDM.COLUMN_TEAM, PlayerDM.COLUMN_SALUTATION, PlayerDM.COLUMN_TITLE};
	
	public PlayerDataSource(Context context) {
		playerDM = new PlayerDM(context);
	}
	
	public void open() throws SQLException {
		database = playerDM.getWritableDatabase();
	}
	
	public void close() {
		playerDM.close();
	}
	
	public Player createPlayer(String firstName, String lastName, int team, String salutation, String title){
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(PlayerDM.COLUMN_FIRST_NAME, firstName);
		values.put(PlayerDM.COLUMN_LAST_NAME, lastName);
		values.put(PlayerDM.COLUMN_TEAM, team);
		values.put(PlayerDM.COLUMN_SALUTATION, salutation);
		values.put(PlayerDM.COLUMN_TITLE, title);
		long insertId = database.insert(PlayerDM.TABLE_PLAYER, null, values);
		Cursor cursor = database.query(PlayerDM.TABLE_PLAYER, allColumns, PlayerDM.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Player player = cursorToPlayer(cursor);
		cursor.close();
		return player;
	}
	
	public void deletePlayer(Player player) {
		openDatabaseIfNull();
		long id = player.getId();
		System.out.println("Player deleted with id: " + id);
		database.delete(PlayerDM.TABLE_PLAYER, PlayerDM.COLUMN_ID + " = " + id, null);
	}
	
	public List<Player> getAllPlayersForTeam(int teamID) {
		openDatabaseIfNull();
		List<Player> players = new ArrayList<Player>();
		String[] whereArgs = new String[] {String.valueOf(teamID)};
		Cursor cursor = database.query(PlayerDM.TABLE_PLAYER, allColumns, PlayerDM.COLUMN_TEAM + " = ?", whereArgs, null, null, null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			Player player = cursorToPlayer(cursor);
			players.add(player);
			cursor.moveToNext();
		}
		cursor.close();
		return players;
	}
	
	private Player cursorToPlayer(Cursor cursor) {
		Player player = new Player();
		player.setId(cursor.getLong(0));
		player.setFirstName(cursor.getString(1));
		player.setLastName(cursor.getString(2));
		player.setTeam(cursor.getLong(3));
		player.setSalutation(cursor.getString(4));
		player.setTitle(cursor.getString(5));
		return player;
	}
	
	private void openDatabaseIfNull(){
		if(database==null){
			open();
		}
	}

	public Player getPlayerByID(int playerID) {
		openDatabaseIfNull();
		String[] whereArgs = new String[] {String.valueOf(playerID)};
		Cursor cursor = database.query(PlayerDM.TABLE_PLAYER, allColumns, PlayerDM.COLUMN_ID + " = ?", whereArgs, null, null, null);
		cursor.moveToFirst();
		Player player = cursorToPlayer(cursor);
		cursor.close();
		return player;
	}
	
	public void updatePlayer(Player player) {
		openDatabaseIfNull();
		ContentValues values = buildContentValues(player);
		String[] whereArgs = new String[] {String.valueOf(player.getId())};
		database.update(PlayerDM.TABLE_PLAYER, values, PlayerDM.COLUMN_ID + " = ?", whereArgs);
	}

	private ContentValues buildContentValues(Player player) {
		ContentValues values = new ContentValues();
		values.put(PlayerDM.COLUMN_FIRST_NAME, player.getFirstName());
		values.put(PlayerDM.COLUMN_LAST_NAME, player.getLastName());
		values.put(PlayerDM.COLUMN_TEAM, player.getTeam());
		values.put(PlayerDM.COLUMN_SALUTATION, player.getSalutation());
		values.put(PlayerDM.COLUMN_TITLE, player.getTitle());
		return values;
	}

}
